<?php

/**
 * Tanaxia_Loader.php
 *
 * The Tanaxia_Loader class that will let us load controllers, models, views, helpers, configs and libraries
 * 
 * Copyright 2009, Marcus Schumann - Tanax
 * Licensed under The MIT License - Modification, Redistribution allowed but must retain the above copyright notice
 * @link 		http://www.opensource.org/licenses/mit-license.php
 *
 * @package 	Tanaxia_Loader - TAF
 * @created 	Mars 26th 2009
 * @version 	1.0
 * @link		-
 * 
 * @changelog
 * - 09/03/26	v1.0	Initial release
 */

class Tanaxia_Loader
{
	
	/*
	|--------------------------------------------
	| $viewVars
	|--------------------------------------------
	|
	| An array of variables usable from the view file
	|
	*/
	private $viewVars = array();
	
	/*
	|--------------------------------------------------------------------------
	| controllers
	|--------------------------------------------------------------------------
	|
	| Loads and run a controllers
	|
	*/
	public function controller($controller, $function = NULL, $params = NULL)
	{
		
		if(!file_exists(CONTROLLERS . $controller . EXT))
		{
			
			die('Controller ' . $controller . ' does not exist');
			
		}
		
		include(CONTROLLERS . $controller . EXT);
		
		$name = ucfirst($controller) . "Controller";
		$class =& new $name();
		
		if($function === NULL)
		{
			
			return $class->index();
			
		}
		
		elseif($params !== NULL)
		{
			
			return $class->$function($params);
			
		}
		
		else
		{
			
			return $class->$function();
			
		}
		
	}
	
	/*
	|--------------------------------------------------------------------------
	| model
	|--------------------------------------------------------------------------
	|
	| Loads a model
	|
	*/
	public function model($model, $name = null)
	{
		
		Tanaxia_Core::loadModel($model, $name);	
		return true;
		
	}
	
	/*
	|--------------------------------------------------------------------------
	| view
	|--------------------------------------------------------------------------
	|
	| Includes a view
	|
	*/
	public function view($view, $params = NULL)
	{
		if($params !== NULL)
		{
			
			$this->viewVars = $params;
			
		}
		
		if(!file_exists(VIEWS . $view . EXT))
		{
			
			die('Viewfile ' . $view . ' does not exist.');
			
		}
		
		include(VIEWS . $view . EXT);
		unset($this->viewVars);
		
	}
	
	/*
	|--------------------------------------------------------------------------
	| library
	|--------------------------------------------------------------------------
	|
	| Loads a library
	|
	*/
	public function library($library, $name = null, $param = null)
	{
		
		Tanaxia_Core::loadLibrary($library, $name, $param);
		return true;
		
	}
	
	/*
	|--------------------------------------------------------------------------
	| helper
	|--------------------------------------------------------------------------
	|
	| Includes a helper
	|
	*/
	public function helper($helper)
	{
		
		Tanaxia_Core::loadHelper($helper);
		return true;
		
	}
	
	/*
	|--------------------------------------------------------------------------
	| plugin
	|--------------------------------------------------------------------------
	|
	| Includes a plugin
	|
	*/
	public function plugin($plugin, $ref = null, $param = null)
	{
		
		Tanaxia_Core::loadPlugin($plugin, $ref, $param);
		return true;
		
	}
	
	/*
	|--------------------------------------------------------------------------
	| __get
	|--------------------------------------------------------------------------
	|
	| Gets the variable from the viewVars, used in view files
	|
	*/
	public function __get($variable)
	{
		
		if(isset($this->viewVars[$variable]))
		{
			
			return $this->viewVars[$variable];
			
		}
		
		return false;
		
	}
	
}


// End of Tanaxia_Loader.php